package org.ogv.tween;

public class Tween {
	public float curTime;
	public float beginPos;
	public float endPos;
	public float duration;
	private EaseFunc _ease;
	private int _state; // 0 = ended, 1 = running, 2 = completed
	
	public Tween() {

	}
	
	public Tween init(float beginPos, float endPos, float duration, int easeId) {
		curTime 		= 0;
		_state 			= 0;
		this.duration 	= duration;
		this.endPos 	= endPos;
		this.beginPos 	= beginPos;
		_ease 			= EaseFactory.inst().getEase(easeId);
		
		return this;
	}
	
	public void end() {
		_state = 2;
	}
	
	public void update(float elapsedSecs) {
		//Exit if is done
		if(_state == 2) return;
		else if(_state == 0) {
			_state = 1;
		}
		
		//Update time
		curTime += elapsedSecs;
		if(curTime > duration) {
			curTime = duration;
			_state = 2;
		}
	}
	
	public float getCurrentValue() {
		return _ease == null ? beginPos : _ease.getTweenedVal(curTime, beginPos, endPos - beginPos, duration);
	}
	
	public boolean isDone() { return _state == 2; }
	
	public boolean isRunning() { return _state == 1; }
}
